package com.example.test;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.test.db.DBOpenHelper;
import com.example.test.db.User;

import java.util.List;

public class MainActivity extends AppCompatActivity{
   private Button btnRegister,btnLogin;
   private EditText userPhone,userPwd,userName;
   private String phone,pwd;
   private String name;
    private String paw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        btnRegister = findViewById(R.id.btnRegister);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        btnLogin = findViewById(R.id.btnLogin);
        userName = findViewById(R.id.userName);
        userPhone = findViewById(R.id.userPhone);
        userPwd = findViewById(R.id.userPwd);
        btnLogin =findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name=userName.getText().toString();
                pwd=userPwd.getText().toString();
                DBOpenHelper dbOpenHelper=new DBOpenHelper(MainActivity.this);
                List<User> list=dbOpenHelper.query(name);
                for (User user:list){
                    name=user.getName();
                    phone=user.getPhone();
                    paw=user.getPassword();
                }
                if(!name.equals("")){
                    if (name.equals(name)){
                        if (pwd.equals(paw)){
                            Intent intent=new Intent();
                            intent.setClass(MainActivity.this,HomeActivity.class);
                            intent.putExtra("name",name);
                            intent.putExtra("phone",phone);
                            startActivity(intent);
                        }else {
                            Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(MainActivity.this,"账号不存在",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(MainActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();

                }
            }
        });
//
//
//
    }


}